<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/4/27 0027
 * Time: 22:29
 */

namespace logicmodel;


use comservice\Response;
use datamodel\Box;
use datamodel\BoxOrders;
use datamodel\Goods;
use datamodel\GoodsOrders;
use datamodel\OrdersBox;
use datamodel\OrdersGoods;
use datamodel\UsersBox;
use datamodel\UsersGoods;
use think\Db;

class TaskLogic
{


    /**
     * 设置短信验证码
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setCode(){
        $code = rand(111111,999999);
        $result = Db::name('config')->where(['id'=>20])->update(['value'=>$code]);
        if($result) return Response::success('设置成功');
        return Response::fail('设置失败');
    }

    /**
     * 取消盲盒订单
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cancelOrderBox(){
        $time = date('Y-m-d H:i:s',strtotime('-2 minutes'));
       $ordersBoxData =  new OrdersBox();
       $ordersData =  $ordersBoxData->where(['status'=>0,'create_time'=>['<=',$time]])->select();

       $boxData = new Box();
       $cancel = [];
       foreach ($ordersData as $v){
           $box_id = $v['box_id'];
           $boxData->where(['id'=>$box_id])->setInc('surplus');
           $boxData->where(['id'=>$box_id])->setDec('sales');
           $cancel[] = $v['id'];
       }
        $ordersBoxData->where(['id'=>['in',$cancel]])->update(['status'=>-1]);
       return Response::success('取消成功');
    }

    /**
     * 取消NFT订单
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cancelOrderGoods(){
        $time = date('Y-m-d H:i:s',strtotime('-2 minutes'));
        $ordersGoodsData =  new OrdersGoods();
        $ordersData =  $ordersGoodsData->where(['status'=>0,'create_time'=>['<=',$time]])->select();
        $goodsData = new Goods();
        $manage = [];
        $cancel = [];
        foreach ($ordersData as $v){
            $goods_id = $v['goods_id'];
            if($v['number'] > 1) $manage[] = $goods_id;
            $goodsData->where(['id'=>$goods_id])->setInc('surplus',$v['number']);
            $cancel[] = $v['id'];
        }
        $ordersGoodsData->where(['id'=>['in',$cancel]])->update(['status'=>-1]);
        if(count($manage)) $goodsData->where(['id'=>['in',$manage]])->update(['is_manage'=>0]);
        return Response::success('取消成功');
    }

    /**
     * 交易订单取消
     * @return array
     */
    public function cancelGoodsOrder(){
        $time = date('Y-m-d H:i:s',strtotime('-1 minutes'));
        $goodsOrdersData =  new GoodsOrders();
        $orders =  $goodsOrdersData->where(['status'=>0,'create_time'=>['<=',$time]])->column('id');
        if(count($orders) <= 0) return  Response::fail('暂未有订单未付款');
        $goodsOrdersData->where(['id'=>['in',$orders]])->update(['status'=>-1]);
        $goods = $goodsOrdersData->where(['id'=>['in',$orders]])->column('users_goods_id');
        (new UsersGoods())->where(['id'=>['in',$goods]])->update(['status'=>2]);
        return Response::success('取消成功');
    }

    /**
     * 交易订单取消
     * @return array
     */
    public function cancelBoxOrder(){
        $time = date('Y-m-d H:i:s',strtotime('-1 minutes'));
        $boxOrdersData =  new BoxOrders();
        $orders =  $boxOrdersData->where(['status'=>0,'create_time'=>['<=',$time]])->column('id');
        if(count($orders) <= 0) return  Response::fail('暂未有订单未付款');
        $boxOrdersData->where(['id'=>['in',$orders]])->update(['status'=>-1]);
        $box = $boxOrdersData->where(['id'=>['in',$orders]])->column('users_box_id');
        (new UsersBox())->where(['id'=>['in',$box]])->update(['status'=>2]);
        return Response::success('取消成功');
    }
}